home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xarchie-2.0.9 / help.c < prev    next >
C/C++ Source or Header  |  1995-06-18  |  7KB  |  270 lines

  1. /*
  2.  * help.c : The Help browser
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 23 Apr 1993.
  5.  * 13 May 1993: Change the way the version number is printed.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <X11/Intrinsic.h>
  10. #include <X11/Shell.h>
  11. #include <X11/StringDefs.h>
  12. #include <X11/Xaw/Form.h>
  13. #include <X11/Xaw/Label.h>
  14. #include <X11/Xaw/Viewport.h>
  15. #include <X11/Xaw/List.h>
  16. #include <X11/Xaw/AsciiText.h>
  17. #include <X11/Xaw/Command.h>
  18. #include <X11/Xaw/Cardinals.h>
  19. #include "xarchie.h"
  20. #include "xutil.h"
  21. #include "patchlevel.h"
  22.  
  23. /*
  24.  * Functions defined here
  25.  */
  26. void initHelpActions(),initHelpPanel();
  27. void setHelpShellState();
  28.  
  29. static void popupHelpPanel();
  30. static void initHelpWidgets();
  31. static void helpCallback();
  32. static void helpDoneAction(),helpPrevAction(),helpNextAction();
  33. static void helpDownAction(),helpUpAction();
  34.  
  35. /*
  36.  * Data defined here
  37.  */
  38. static Widget helpShell;
  39. static Widget helpLabel,helpViewport,helpList,helpText;
  40. static Widget helpScrollbar;
  41. static Widget helpPrevButton,helpNextButton;
  42.  
  43. static Boolean isPoppedUp;
  44. static int numHelpTopics;
  45.  
  46. static XtActionsRec actionTable[] = {
  47.     { "help",        popupHelpPanel },
  48.     { "help-done",    helpDoneAction },
  49.     { "help-prev",    helpPrevAction },
  50.     { "help-next",    helpNextAction },
  51.     { "help-down",    helpDownAction },
  52.     { "help-up",    helpUpAction },
  53. };
  54.  
  55. static char *helpStrings[] = {
  56. #include "help-text1.h"
  57.     NULL
  58. };
  59.  
  60. static struct {
  61.     int lineno;
  62.     char *string;
  63. } helpTopicInfo[] = {
  64. #include "help-text2.h"
  65. };
  66.  
  67. static char **helpTopics;
  68.  
  69. /*    -    -    -    -    -    -    -    -    */
  70.  
  71. void
  72. initHelpActions()
  73. {
  74.     XtAppAddActions(appContext,actionTable,XtNumber(actionTable));
  75. }
  76.  
  77. void
  78. initHelpPanel()
  79. {
  80.     int i;
  81.  
  82.     /* Last entry in help-text2.h is bogus */
  83.     numHelpTopics = XtNumber(helpTopicInfo)-1;
  84.     /* Make an array with just the strings for the List widget */
  85.     helpTopics = (char **)XtCalloc(numHelpTopics,sizeof(char*));
  86.     for (i=0; i < numHelpTopics; i++)
  87.     *(helpTopics+i) = helpTopicInfo[i].string;
  88. }
  89.  
  90. static void
  91. popupHelpPanel()
  92. {
  93.     if (isPoppedUp) {
  94.     XRaiseWindow(display,XtWindow(helpShell));
  95.     return;
  96.     }
  97.     if (helpShell == NULL) {
  98.     initHelpWidgets();
  99.     }
  100.     isPoppedUp = True;
  101.     XtPopup(helpShell,XtGrabNone);
  102. }
  103.  
  104. static void
  105. initHelpWidgets()
  106. {
  107.     Widget form;
  108.     char buf[64];
  109.  
  110.     helpShell = XtCreatePopupShell("helpShell",topLevelShellWidgetClass,
  111.                    toplevel,NULL,0);
  112.     form = XtCreateManagedWidget("helpForm",formWidgetClass,
  113.                  helpShell,NULL,0);
  114.     helpLabel = XtCreateManagedWidget("helpLabel",labelWidgetClass,
  115.                       form,NULL,0);
  116. #ifdef BETA
  117.     sprintf(buf,"This is help for Xarchie %.1fb%d",VERSION,PATCHLEVEL);
  118. #else
  119.     sprintf(buf,"This is help for Xarchie %.1f.%d",VERSION,PATCHLEVEL);
  120. #endif
  121.     setWidgetLabel(helpLabel,buf);
  122.     helpViewport = XtCreateManagedWidget("helpViewport",viewportWidgetClass,
  123.                      form,NULL,0);
  124.     helpList = XtCreateManagedWidget("helpList",listWidgetClass,
  125.                      helpViewport,NULL,0);
  126.     XawListChange(helpList,helpTopics,numHelpTopics,0,True);
  127.     XtAddCallback(helpList,XtNcallback,helpCallback,NULL);
  128.     helpText = XtCreateManagedWidget("helpText",asciiTextWidgetClass,
  129.                      form,NULL,0);
  130.     (void)XtCreateManagedWidget("helpDoneButton",commandWidgetClass,
  131.                 form,NULL,0);
  132.     helpPrevButton = XtCreateManagedWidget("helpPrevButton",commandWidgetClass,
  133.                        form,NULL,0);
  134.     XtSetSensitive(helpPrevButton,False);
  135.     helpNextButton = XtCreateManagedWidget("helpNextButton",commandWidgetClass,
  136.                        form,NULL,0);
  137.     XtSetSensitive(helpNextButton,False);
  138.     (void)XtCreateManagedWidget("helpDownButton",commandWidgetClass,
  139.                 form,NULL,0);
  140.     (void)XtCreateManagedWidget("helpUpButton",commandWidgetClass,
  141.                 form,NULL,0);
  142.     XtRealizeWidget(helpShell);
  143.     (void)XSetWMProtocols(XtDisplay(helpShell),XtWindow(helpShell),
  144.               &WM_DELETE_WINDOW,1);
  145.     helpScrollbar = XtNameToWidget(helpViewport,"vertical");
  146. }
  147.  
  148. void
  149. setHelpShellState(state)
  150. int state;
  151. {
  152.     if (!isPoppedUp)
  153.     return;
  154.     switch (state) {
  155.     case NormalState:
  156.         XtMapWidget(helpShell);
  157.         break;
  158.     case IconicState:
  159.         XtUnmapWidget(helpShell);
  160.         break;
  161.     }
  162. }
  163.  
  164. /*    -    -    -    -    -    -    -    -    */
  165. /* Callback procedure */
  166.  
  167. /*ARGSUSED*/
  168. static void
  169. helpCallback(w,client_data,call_data)
  170. Widget w;
  171. XtPointer client_data;  /* not used */
  172. XtPointer call_data;    /* returnStruct */
  173. {
  174.     int topic = ((XawListReturnStruct*)call_data)->list_index;
  175.     XawTextPosition pos;
  176.     XawTextBlock block;
  177.     Arg args[2];
  178.     int i;
  179.  
  180.     block.firstPos = 0;
  181.     block.format = FMT8BIT;
  182.     /* Reset helpText */
  183.     XtSetArg(args[0],XtNstring,"");
  184.     XtSetArg(args[1],XtNeditType,XawtextEdit);
  185.     XtSetValues(helpText,args,2);
  186.     pos = (XawTextPosition)0;
  187.     XawTextDisableRedisplay(helpText);
  188.     for (i=helpTopicInfo[topic].lineno; helpStrings[i] != NULL; i++) {
  189.     /* Add helpStrings[i] to helpText */
  190.     block.ptr = helpStrings[i];
  191.     block.length = strlen(helpStrings[i]);
  192.     XawTextReplace(helpText,pos,pos,&block);
  193.     pos += block.length;
  194.     }
  195.     XawTextEnableRedisplay(helpText);
  196.     XtSetArg(args[0],XtNeditType,XawtextRead);
  197.     XtSetValues(helpText,args,1);
  198.     XtSetSensitive(helpPrevButton,(topic != 0));
  199.     XtSetSensitive(helpNextButton,(topic != numHelpTopics-1));
  200. }
  201.  
  202.  
  203. /*    -    -    -    -    -    -    -    -    */
  204. /* Action procedures */
  205.  
  206. #define ACTION_PROC(NAME)    void NAME(w,event,params,num_params) \
  207.                     Widget w; \
  208.                     XEvent *event; \
  209.                     String *params; \
  210.                     Cardinal *num_params;
  211.  
  212. /*ARGSUSED*/
  213. static
  214. ACTION_PROC(helpDoneAction)
  215. {
  216.     XtPopdown(helpShell);
  217.     isPoppedUp = False;
  218. }
  219.  
  220. /*ARGSUSED*/
  221. static
  222. ACTION_PROC(helpPrevAction)
  223. {
  224.     XawListReturnStruct *ret = XawListShowCurrent(helpList);
  225.     float percent;
  226.  
  227.     if (ret->list_index != XAW_LIST_NONE && ret->list_index != 0) {
  228.     ret->list_index -= 1;
  229.     /* Highlight the item */
  230.     XawListHighlight(helpList,ret->list_index);
  231.     /* Get the text displayed */
  232.     helpCallback(helpList,NULL,(XtPointer)ret);
  233.     /* Adjust the scrollbar so it's visible */
  234.     percent = (float)(ret->list_index-1) / (float)numHelpTopics;
  235.     XtCallCallbacks(helpScrollbar,"jumpProc",(XtPointer)&percent);
  236.     }
  237. }
  238.  
  239. /*ARGSUSED*/
  240. static
  241. ACTION_PROC(helpNextAction)
  242. {
  243.     XawListReturnStruct *ret = XawListShowCurrent(helpList);
  244.     float percent;
  245.  
  246.     if (ret->list_index != XAW_LIST_NONE &&
  247.     ret->list_index != numHelpTopics-1) {
  248.     ret->list_index += 1;
  249.     XawListHighlight(helpList,ret->list_index);
  250.     helpCallback(helpList,NULL,(XtPointer)ret);
  251.     percent = (float)(ret->list_index-1) / (float)numHelpTopics;
  252.     XtCallCallbacks(helpScrollbar,"jumpProc",(XtPointer)&percent);
  253.     }
  254. }
  255.  
  256. /*ARGSUSED*/
  257. static
  258. ACTION_PROC(helpDownAction)
  259. {
  260.     XtCallActionProc(helpText,"next-page",event,NULL,0);
  261. }
  262.  
  263. /*ARGSUSED*/
  264. static
  265. ACTION_PROC(helpUpAction)
  266. {
  267.     XtCallActionProc(helpText,"previous-page",event,NULL,0);
  268. }
  269.  
  270.